#include <QtDebug>
#include <QBrush>
#include <QThread>
#include "process_data_table_model.h"

#include "../data/track_id_data_integrated.h"
#include "../data/constant_variables.h"

Process_data_table_model::Process_data_table_model(QObject *parent):
    QAbstractTableModel(parent),
    process_list_(new QList<Track_id_data_integrated* >())
{
    init_connections();
}

Process_data_table_model::~Process_data_table_model()
{
    if (nullptr != process_list_) {
        delete process_list_;
        process_list_ = nullptr;
    }
}

int Process_data_table_model::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)

    int row_count(0);

    if ( !process_list_->isEmpty()) {
        row_count = process_list_->count();
    }

    return row_count;
}

int Process_data_table_model::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)

    return 17;
}

QVariant Process_data_table_model::data(const QModelIndex &index, int role) const
{
    if ( !index.isValid()) {
        return QVariant();
    }

    QVariant v = QVariant();

    // 获取对应行处数据序列号
    QMap<QString, QString>* data = (process_list_->at(index.row()))->get_track_data();
//    qDebug()<< index.row() << Qt::endl;

    if (Qt::DisplayRole == role) {
        switch (index.column()) {
        case 0:
            if ( data->contains(TRACK_ID)) {
                v = data->value(TRACK_ID);
            } else {
                v = NONE;
            }
            break;

        case 1:
            if ( data->contains(TIME)) {
                v = data->value(TIME);
            } else {
                v = NONE;
            }
            break;

        case 2:
            if ( data->contains(CALL_SIGN)) {
                v = data->value(CALL_SIGN);
            } else {
                v = NONE;
            }
            break;

        case 3:
            if ( data->contains(SSR_CODE)) {
                v = data->value(SSR_CODE);
            } else {
                v = NONE;
            }
            break;

        case 4:
            if ( data->contains(H0)) {
                v = data->value(H0);
            } else if (data->contains("h")) {
                v = (data->value("h") + ", QNH Applied");
            } else {
                v = NONE;
            }
            break;

        case 5:
            if ( data->contains(CFL)) {
                v = data->value(CFL);
            } else {
                v = NONE;
            }
            break;

        case 6:
            if ( data->contains(LONGITUDE)) {
                v = data->value(LONGITUDE);
            } else {
                v = NONE;
            }
            break;

        case 7:
            if ( data->contains(LATITUDE)) {
                v = data->value(LATITUDE);
            } else {
                v = NONE;
            }
            break;

        case 8:
            if ( data->contains(SPEED)) {
                v = data->value(SPEED);
            } else {
                v = NONE;
            }
            break;

        case 9:
            if ( data->contains(HEADING)) {
                v = data->value(HEADING);
            } else {
                v = NONE;
            }
            break;

        case 10:
            if ( data->contains(ARR_AIRPORT)) {
                v = data->value(ARR_AIRPORT);
            } else {
                v = NONE;
            }
            break;

        case 11:
            if ( data->contains(DEP_AIRPORT)) {
                v = data->value(DEP_AIRPORT);
            } else {
                v = NONE;
            }
            break;

        case 12:
            if ( data->contains(EOBT)) {
                v = data->value(EOBT);
            } else {
                v = NONE;
            }
            break;

        case 13:
            if ( data->contains(RVSM)) {
                v = data->value(RVSM);
            } else {
                v = NONE;
            }
            break;

        case 14:
            if ( data->contains(FR)) {
                v = data->value(FR);
            } else {
                v = NONE;
            }
            break;

        case 15:
            if ( data->contains(ACTF_TURB)) {
                v = data->value(ACTF_TURB);
            } else {
                v = NONE;
            }
            break;

        case 16:
            if ( data->contains(TIME_STAMP)) {
                v = data->value(TIME_STAMP);
            } else {
                v = NONE;
            }
            break;
        }

        return v;
    }

    if (Qt::BackgroundRole == role) {
        switch (index.row() % 2) {
        case 0:
            v = QBrush(Qt::yellow, Qt::SolidPattern);
            break;

        case 1:
            v = QBrush(Qt::green, Qt::SolidPattern);
            break;
        }

        return v;
    }

    return QVariant();
}

QVariant Process_data_table_model::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (Qt::DisplayRole == role && Qt::Horizontal == orientation) {
        QString header;

        switch (section) {
        case 0:
            header = TRACK_ID;
            break;

        case 1:
            header = TIME;
            break;

        case 2:
            header = CALL_SIGN;
            break;

        case 3:
            header = SSR_CODE;
            break;

        case 4:
            header = H0;
            break;

        case 5:
            header = CFL;
            break;

        case 6:
            header = LONGITUDE;
            break;

        case 7:
            header = LATITUDE;
            break;

        case 8:
            header = SPEED;
            break;

        case 9:
            header = HEADING;
            break;

        case 10:
            header = ARR_AIRPORT;
            break;

        case 11:
            header = DEP_AIRPORT;
            break;

        case 12:
            header = EOBT;
            break;

        case 13:
            header = RVSM;
            break;

        case 14:
            header = FR;
            break;

        case 15:
            header = ACTF_TURB;
            break;

        case 16:
            header = TIME_STAMP;
            break;
        }

        return header;
    }

    return QAbstractTableModel::headerData(section, orientation, role);
}

void Process_data_table_model::reset_data_set()
{
    if (0 != process_list_->size()) {
        delete process_list_;

        process_list_ = new QList<Track_id_data_integrated*>();
    }
}

void Process_data_table_model::init_connections()
{
    connect(this, &Process_data_table_model::sig_get_track_data_integrated,
            this, &Process_data_table_model::add_data_to_process_list);
}

void Process_data_table_model::add_data_to_process_list(Track_id_data_integrated* data)
{
//    qDebug()<< "Model thread: " << (quint64) QThread::currentThreadId() << Qt::endl;

    process_list_->append(data);

    // 给模型添加数据后，发出刷新信号
    emit layoutChanged();

}
